import socket
from time import sleep
class EthernetPortTool(object): 
  
    def __init__(self,IPAddress,EthPort,debugPrint):
        self.EthDevice = None
        self.deviceip = IPAddress
        self.deviceport = EthPort
        self.debugprint = debugPrint
        self.rcvLine = ""
        self.DCVolRange1V   = "CONF:VOLT:DC 1, 0.0001;\r\n"
        self.DCVolRange10V  = "CONF:VOLT:DC 10, 0.0001;\r\n" 
        self.DCVolRange100V = "CONF:VOLT:DC 100, 0.0001;\r\n"
        self.DCCurrent1A    = "CONF:CURR:DC 1, 0.0001;\r\n"
        self.DCCurrent3A    = "CONF:CURR:DC 3, 0.0001;\r\n"
        self.DCCurrent01A   = "CONF:CURR:DC 0.1, 0.0001;\r\n"        
        self.measure = "READ?;\r\n"
    
    def ConnectEthDevice(self):
        self.EthDevice = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.EthDevice.connect((self.deviceip,self.deviceport))
        
    
    def EthDeviceReceive(self):
        self.rcvLine = self.EthDevice.recv(100).strip()
        return self.rcvLine

    def Keysight34461AMeas(self):
        self.EthDevice.send(self.measure)
        sleep(0.01)
        self.rcvLine = self.EthDevice.recv(100).strip()
        self.EthDevice.send(self.measure)
        sleep(0.01)
        self.rcvLine = self.EthDevice.recv(100).strip()
        return self.rcvLine
    
    def EthDeviceSend(self,message):
        self.EthDevice.send(message)
        
    def Keysight34461ARangeSet(self,model):
        if model == "V1":
            self.EthDeviceSend(self.DCVolRange1V)
        if model == "V10":
            self.EthDeviceSend(self.DCVolRange10V) 
        if model == "V100":
            self.EthDeviceSend(self.DCVolRange100V)          
        if model == "C0.1":
            self.EthDeviceSend(self.DCCurrent01A)
        if model == "C1":
            self.EthDeviceSend(self.DCCurrent1A)
        if model == "C3":
            self.EthDeviceSend(self.DCCurrent3A)              